package com.epoint.mobileoa.ui.notice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.GetNoticeListTask;
import com.epoint.mobileoa.datanalysis.XMLHelp;
import com.epoint.mobileoa.model.NoticeList;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.PropertiesHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;

/**
 * @author liyc
 * @time 2012-2-4 上午9:33:47
 * @annotation 通知公告列表
 */
public class NoticeListView extends SuperView {

	public static final int GetNoticeList = 1;

	public static int allNoticeNums;//通知公告总数量

	private ListView lv;

	private LinearLayout foot;

	private int shownum = 20;//每页显示数量
	private int pagenum = 1;//第几页

	private List<NoticeList> allList = new ArrayList<NoticeList>();
	
	private MyAdapter adapter = new MyAdapter();
	@Override
	public void initControls() {
		lv = (ListView) findViewById(R.id.lvNotice);
		lv.setOnItemClickListener(this);
		addFoot();
		
		lv.setAdapter(adapter);
		
		getData(pagenum,shownum);
		
		lv.setVisibility(View.INVISIBLE);
	}

	public void getData(int pagenum, int shownum){
		top_pb.setVisibility(View.VISIBLE);
		HashMap<String,Object> param=new HashMap<String,Object>();
		param.put("pagenum", pagenum);
		param.put("shownum", shownum);
		Task task = new GetNoticeListTask(this, param);
		MainService.addTask(task);
	}

	public void addFoot(){
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			lv.setVisibility(View.VISIBLE);
			
			String bs = param[1].toString();
			NoticeListView.allNoticeNums = Integer.parseInt(StringHelp.getXMLAtt(bs, "allnum"));
			String content =  StringHelp.getXMLAttOut(bs, "getnotices");
			List<NoticeList> noticelist = XMLHelp.NoticeListAna(content);

			for(NoticeList n:noticelist){
				allList.add(n);
			}
			
			if(noticelist.size()==0){
				ToastHelp.showToastCommon(this, "数据加载完毕");
			}
			
			adapter.notifyDataSetChanged();

		}
		top_pb.setVisibility(View.INVISIBLE);
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		if(position==allList.size()){
			pagenum ++;
			getData(pagenum, shownum);
			return;
		}
		Intent it = new Intent(this,NoticeDetailView.class);
		it.putExtra("pagetitle", "公告详情");
		it.putExtra("title", allList.get(position).title);
		it.putExtra("guid",allList.get(position).guid);
		it.putExtra("sender", allList.get(position).sender);
		it.putExtra("time", allList.get(position).time);
		it.putExtra("hasattach", allList.get(position).hasattach);
		startActivity(it);
	}

	@Override
	public int getLayoutInt() {
		return R.layout.noticelistview;
	}

	@Override
	public String setTitle() {
		return getString(R.string.tzgg);
	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return allList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			LayoutInflater mInflater = LayoutInflater.from(NoticeListView.this);
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.noticelistlayout, null);
				holder.ml_attach = (ImageView) convertView
						.findViewById(R.id.ivAttach);
				holder.ml_mailtitle = (TextView) convertView
						.findViewById(R.id.tvTitle);
				holder.ml_sandt = (TextView) convertView
						.findViewById(R.id.tvSender);
				holder.notice_time = (TextView) convertView
						.findViewById(R.id.tvDate);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			if ("1".equals(allList.get(position).hasattach)) {
				holder.ml_attach.setVisibility(View.VISIBLE);
			}
			else{
				holder.ml_attach.setVisibility(View.INVISIBLE);
			}

			holder.ml_mailtitle.setText(allList.get(position).title);
			setTextViewFontColor(holder.ml_mailtitle);
			String sender = allList.get(position).sender;
			String time =allList.get(position).time;
			//			Spanned h = Html.fromHtml("<font color='#00FFCC'>" + sender
			//					+ "</font>&nbsp;&nbsp;" + "<font color=yellow>" + time
			//					+ "</font>");
			holder.ml_sandt.setText(sender);
			holder.notice_time.setText(Html.fromHtml("<font color='"+getYGColor()+"'>"+time+"</font>"));
			return convertView;
		}

		public class ViewHolder {
			public ImageView ml_attach;
			public TextView ml_mailtitle;
			public TextView ml_sandt;
			public TextView notice_time;
		}
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}